﻿
/*
 *
 * Copyright (C) 2009 Mattias Blomqvist, patr-blo at dsv dot su dot se
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

using FlowLib.Interfaces;
using FlowLib.Events;
using FlowLib.Containers.UPnP;
using FlowLib.Containers.UPnP.Services;

namespace ClientExample
{
    public class UPnPTester : IUPnPUpdater
    {
        public event FlowLib.Events.FmdcEventHandler UpdateBase;
        protected FlowLib.Connections.UPnP upnp;

        public UPnPTester()
        {
            UpdateBase = new FlowLib.Events.FmdcEventHandler(OnUpdateBase);
            upnp = new FlowLib.Connections.UPnP(this);
            upnp.ProtocolUPnP.Update += new FlowLib.Events.FmdcEventHandler(OnUpdate);
            int i = 0;
            do
            {
                upnp.Discover();
                System.Threading.Thread.Sleep(5 * 1000);
            } while (i < 5);
        }

        void OnUpdateBase(object sender, FlowLib.Events.FmdcEventArgs e) { }

        public void FireUpdateBase(FlowLib.Events.FmdcEventArgs e)
        {
            UpdateBase(this, e);
        }

        protected void OnUpdate(object sender, FlowLib.Events.FmdcEventArgs e)
        {
            FlowLib.Connections.UPnP upnp = sender as FlowLib.Connections.UPnP;
            FlowLib.Containers.UPnP.UPnPDevice device = e.Data as FlowLib.Containers.UPnP.UPnPDevice;
            if (upnp != null && device != null)
            {
                switch (e.Action)
                {
                    case Actions.UPnPRootDeviceFound:
                        System.Console.WriteLine("Device:" + device.Information.ToString());
                        FlowLib.Events.FmdcEventArgs e2 = new FlowLib.Events.FmdcEventArgs(Actions.UPnPDeviceDescription, device.Information.Sender.ToString());
                        UpdateBase(this, e2);
                        break;
                    case Actions.UPnPDeviceUpdated:
                        foreach (ServiceBase service in device.Services)
                        {
                            System.Console.WriteLine("Service:" + service.Information.serviceType);
                            if (service.Information.serviceType != null && service.Information.serviceType.Contains("WANIPConnection"))
                            {
                                WANIPConnectionService wanipService = new WANIPConnectionService(service);
                                #region GetExternalIPAddress
                                System.Console.WriteLine("GetExternalIPAddress:" + wanipService.GetExternalIPAddress(this).ToString());
                                #endregion
                                #region AddPortMapping
                                WANIPConnectionService.PortMapping mapping = new WANIPConnectionService.PortMapping(
                                    string.Empty,
                                    31337,
                                    "TCP",
                                    31337,
                                    string.Empty,
                                    true,
                                    "TestUPnPForward",
                                    0
                                    );
                                System.Console.WriteLine("AddPortMapping:" + wanipService.AddPortMapping(this, mapping).ToString());
                                #endregion
                                #region DeletePortMapping
                                System.Console.WriteLine("DeletePortMapping:" + wanipService.DeletePortMapping(this, mapping));
                                #endregion
                            }
                        }
                        break;
                }
            }
        }
    }
}
